//
//FOC.H
// Created by Xiaoyun on 2023/3/28.
//

/**
 * -->PWM(a,b,c)[float 0.,1. _]
 * <--Current_ADC(a,b,c)[float _ A]
 * <--Encoder_Angle[float -180.,180. degrees]
 * <-->Sin,Cos[float -180.,180. degrees]
 */


#ifndef FOC_FOC_H
#define FOC_FOC_H

#include <stdint.h>
#include "foc_config.h"

#ifndef __weak
#define __weak   __attribute__((weak))
#endif

#define FOC_ANGLE_MAX (180.f)
#define FOC_ANGLE_MIN (-180.f)

#define C_SQRT_3 (1.73205080757f)
#define C_PI  (3.14159265359f)

typedef struct {
  float P;
  float I;
} PI_Param_t;

/*------------------------------User Interface Function------------------------------*/

void FOC_Init();

void FOC_Angle_Calibration();

float FOC_Get_Angle();

float FOC_Get_Elec_Angle();

uint8_t FOC_InvPark_SVPWM(float uq, float ud,float sin_th,float cos_th,float* pwma,float* pwmb,float* pwmc);

void FOC_Clark_Park(float ia,float ib,float ic,float sin_th,float cos_th,float* iq,float* id);

void FOC_PI(float iqs, float ids, float iq, float id, float *uq, float *ud);

void FOC_Closed_Loop(float iq_set,float id_set);
/*------------------------------Weak Function(User to implement)------------------------------*/

void FOC_Bsp_Init();

void FOC_SinCos(float theta, float *sin, float *cos);

void FOC_Set_PWM(float pwm_a, float pwm_b, float pwm_c);

float FOC_Get_Angle();

void FOC_Get_Current(float* ia,float* ib,float* ic);

void FOC_Set_ADC_Trig(float trig_phase);

#endif //FOC_FOC_H
